﻿/*    '
    ' Boogaloo® - http://www.mindfly.com
    ' Copyright (c) 2008
    ' by Mindfly, Inc.
    '
    ' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
    ' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
    ' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
    ' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
    '
    ' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
    ' of the Software.
    '
    ' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
    ' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    ' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    ' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    ' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    ' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    ' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


var Boogaloo = function() {
    return {
        version: '1.0.0.5',
        JSONasmx: '/js/Boogaloo.JSON.asmx',
        SSasmx: '/js/Boogaloo.ScriptService.asmx',
        prm: '',
        panels: '',
        updateFunctions: '',
        keyValuePair: function() {
            return {
                _key: null,
                _value: null,
                key: function(k) {
                    this._key = k;
                },
                value: function(v) {
                    this._value = v;
                }
            }
        },
        dictionary: function() {
            return {
                _items: new Array(),
                add: function(key, value, overwrite) {
                    var index = Boogaloo.dictionary.contains(key);
                    if (index < 0) {
                        var kvp = new Boogaloo.keyValuePair;
                        kvp.key = key;
                        kvp.value = value;
                        this._items.push(kvp);
                        return true;
                    } else if (index >= 0 && overwrite) {
                        this._items[index].value = value;
                        return true;
                    } else {
                        return false;
                    }
                },
                contains: function(key) {
                    var found = false;
                    var i = 0;
                    while (i < this._items.length && !found) {
                        if (this._items[i].key == key) {
                            found = true;
                        } else {
                            i++;
                        }
                    }
                    if (found) {
                        return i;
                    } else {
                        return -1;
                    }
                },
                retrieve: function(key) {
                    var index = Boogaloo.dictionary.contains(key);
                    if (index >= 0) {
                        return Boogaloo.dictionary._items[index].value;
                    } else {
                        return null;
                    }
                },
                count: function() {
                    return Boogaloo.dictionary._items.length;
                }
            }
        } (),
        invoke: function(url, methodName, parameters, callbackFunction, errorFunction) {
            Sys.Net.WebServiceProxy.invoke(url, methodName, false, parameters, callbackFunction, errorFunction);
        },
        addUpdatePanelFunction: function(func) {
            var oldfunc = Boogaloo.updateFunctions;
            if (typeof Boogaloo.updateFunctions != 'function') {
                Boogaloo.updateFunctions = func;
            } else {
                Boogaloo.updateFunctions = function() {
                    oldfunc();
                    func();
                }
            }
        },
        initAjaxFixes: function() {
            prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_pageLoaded(Boogaloo.pageLoaded);
        },
        pageLoaded: function(sender, args) {
            if (Boogaloo.panels != undefined && Boogaloo.panels.length > 0) {
                for (i = 0; i < Boogaloo.panels.length; i++)
                    Boogaloo.panels[i].dispose();
            }
            var p = args.get_panelsUpdated();
            if (p.length > 0) {
                Boogaloo.updateFunctions();
            }
        },
        JSON: {
            getNode: function(node_id, callbackFunction, errorFunction) {
                Boogaloo.invoke(Boogaloo.JSONasmx, 'GetNode', { 'node_id': node_id }, callbackFunction, errorFunction);
            },
            getNodes: function(root_id, callbackFunction, errorFunction) {
                Boogaloo.invoke(Boogaloo.JSONasmx, 'GetNodes', { 'root_id': root_id }, callbackFunction, errorFunction);
            },
            getChildNodes: function(parent_id, callbackFunction, errorFunction) {
                Boogaloo.invoke(Boogaloo.JSONasmx, 'GetChildNodes', { 'parent_id': root_id }, callbackFunction, errorFunction);
            },
            getDefaultNode: function(root_id, callbackFunction, errorFunction) {
                Boogaloo.invoke(Boogaloo.JSONasmx, 'GetDefaultNode', { 'root_id': root_id }, callbackFunction, errorFunction);
            },
            getCurrentNode: function(callbackFunction, errorFunction) {
                Boogaloo.invoke(Boogaloo.JSONasmx, 'GetCurrentNode', null, callbackFunction, errorFunction);
            },
            getParentNode: function(callbackFunction, errorFunction) {
                Boogaloo.invoke(Boogaloo.JSONasmx, 'GetParentNode', null, callbackFunction, errorFunction);
            },
            getChildNodesForMenuItems: function(parent_id, callbackFunction, errorFunction) {
                Boogaloo.invoke(Boogaloo.JSONasmx, 'GetChildNodesForMenuItems', { 'parent_id': parent_id }, callbackFunction, errorFunction);
            },
            getChildNodesForPageLinks: function(parent_id, callbackFunction, errorFunction) {
                Boogaloo.invoke(Boogaloo.JSONasmx, 'GetChildNodesForPageLinks', { 'parent_id': parent_id }, callbackFunction, errorFunction);
            },
            getNodeBreadCrumb: function(node_id, callbackFunction, errorFunction) {
                Boogaloo.invoke(Boogaloo.JSONasmx, 'GetNodeBreadCrumb', { 'node_id': node_id }, callbackFunction, errorFunction);
            }
        },
        ScriptService: {
            domainExists: function(domain_name, callbackFunction, errorFunction) {
                Boogaloo.invoke(Boogaloo.SSasmx, 'DomainExists', { 'domain_name': domain_name }, callbackFunction, errorFunction);
            }
        },
        Manip: {
            bindHiddenTextBoxes: function(selector) {
                $('.inlineEdit label').live('click', function() {
                    // close open boxes
                    $('.inlineEdit.editing').removeClass('editing');
                    // open selected box and store current value of editable field.
                    var paren = $(this).parent();
                    while (!paren.hasClass('inlineEdit')) {
                        paren = paren.parent();
                    }
                    paren.addClass('editing');
                    $(this).siblings('input').bind('focus', function() {
                        Boogaloo.dictionary.add('currentEditField', $(this).attr('value'), true);
                        $(this).unbind('focus');
                    });
                    // set up bind functions for when cancel is hit
                    $(this).siblings('a:contains("Cancel")').bind('click', function() {
                        $(this).siblings('input').attr('value', Boogaloo.dictionary.retrieve("currentEditField"));
                        $('.inlineEdit.editing').removeClass('editing');
                        $(this).unbind('click');
                        return false;
                    });
                });
            }
        }

    }
} ();
